package com.ruoyi.web.controller.training;

import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.base.domain.LiveProductItem;
import com.ruoyi.live.configuration.SessionInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.live.base.domain.TaskModule;
import com.ruoyi.live.base.service.ITaskModuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务模块Controller
 *
 * @author ruoyi
 * @date 2023-02-01
 */
@RestController
@RequestMapping("/training/task_module")
public class TaskModuleController extends BaseController
{
    @Autowired
    private ITaskModuleService taskModuleService;

    /**
     * 查询任务模块列表
     */
    @PreAuthorize("@ss.hasPermi('training:task_module:list')")
    @GetMapping("/list")
    public TableDataInfo list(TaskModule taskModule)
    {
        startPage();
        QueryWrapper<TaskModule> taskModuleQueryWrapper = new QueryWrapper<>(taskModule);
        List<TaskModule> list = taskModuleService.list(taskModuleQueryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出任务模块列表
     */
    @PreAuthorize("@ss.hasPermi('training:task_module:export')")
    @Log(title = "任务模块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskModule taskModule)
    {
        QueryWrapper<TaskModule> taskModuleQueryWrapper = new QueryWrapper<>(taskModule);
        List<TaskModule> list = taskModuleService.list(taskModuleQueryWrapper);
        ExcelUtil<TaskModule> util = new ExcelUtil<TaskModule>(TaskModule.class);
        util.exportExcel(response, list, "任务模块数据");
    }

    /**
     * 获取任务模块详细信息
     */
    @PreAuthorize("@ss.hasPermi('training:task_module:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(taskModuleService.getById(id));
    }

    /**
     * 新增任务模块
     */
    @PreAuthorize("@ss.hasPermi('training:task_module:add')")
    @Log(title = "任务模块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskModule taskModule)
    {
        taskModule.setCreateTime(System.currentTimeMillis());
        taskModule.setCreateUser(SessionInfo.getUserId());
        taskModule.setState(1);
        return toAjax(taskModuleService.save(taskModule));
    }

    /**
     * 修改任务模块
     */
    @PreAuthorize("@ss.hasPermi('training:task_module:edit')")
    @Log(title = "任务模块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskModule taskModule)
    {
        return toAjax(taskModuleService.updateById(taskModule));
    }

    /**
     * 删除任务模块
     */
    @PreAuthorize("@ss.hasPermi('training:task_module:remove')")
    @Log(title = "任务模块", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);
        return toAjax(taskModuleService.removeByIds(list));
    }
}
